<template>
  <div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" :label-position="labelPosition"
      :class="isEdit?'':'formItem'">
      <el-card class="box-card" shadow="never">

        <div slot="header" class="header-card">
          <span class="header-card-title">申请信息</span>
        </div>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="车牌号:" prop="carNo">
              <el-input v-model="ruleForm.carNo" @focus="handleFocus" placeholder="请选择处置车辆" v-if="isEdit" />
              <span v-else>{{ ruleForm.carNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车架号:" prop="vinNo">
              <el-input v-model="ruleForm.vinNo" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.vinNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌:" prop="brandName">
              <el-input v-model="ruleForm.brandName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.brandName }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="车型:" prop="modelName">
              <el-input v-model="ruleForm.modelName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.modelName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车款:" prop="seriesName">
              <el-input v-model="ruleForm.seriesName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.seriesName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆产权:" prop="propertyRightName">
              <el-input v-model="ruleForm.propertyRightName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.propertyRightName }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="登记日期:" prop="registerTime">
              <el-input v-model="ruleForm.registerTime" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.registerTime }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车龄:" prop="carAge">
              <el-input v-model="ruleForm.carAge" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.carAge }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排放标准:" prop="environmentalStandards">
              <el-input v-model="ruleForm.environmentalStandards" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.environmentalStandards }}</span>
            </el-form-item>
          </el-col>
<!--          <el-col :span="8">-->
<!--            <el-form-item label="牌照性质:" prop="licenseNature">-->
<!--              <el-input v-model="ruleForm.licenseNature" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />-->
<!--              <span v-else>{{ ruleForm.licenseNature }}</span>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="8">
            <el-form-item label="燃油类型:" prop="fuelType">
              <el-select v-model="ruleForm.fuelType" disabled v-if="isEdit" style="width: 100%" placeholder="请选择车况等级"
                clearable>
                <el-option v-for="dict in dict.type.fuel_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
              <span v-else>{{ ruleForm.fuelType }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="排量:" prop="liter">
              <el-input v-model="ruleForm.liter" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.liter }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="座位数:" prop="seatNum">
              <el-input v-model="ruleForm.seatNum" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.seatNum }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="档位:" prop="gearPosition">
              <el-input v-model="ruleForm.gearPosition" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.gearPosition }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="颜色:" prop="exteriorColor">
              <el-input v-model="ruleForm.exteriorColor" disabled v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ ruleForm.exteriorColor }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="登记类型:" prop="carType">
              <el-select v-model="ruleForm.carNature" disabled style="width: 100%" v-if="isEdit"
                         placeholder="请选择登记类型" clearable>
                <el-option v-for="dict in dict.type.car_nature" :key="dict.value" :label="dict.label"
                           :value="dict.value" />
              </el-select>
              <span v-else>{{ ruleForm.carType }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="上三年出险金额(元):" prop="accidentAmountThreeYears">
              <el-input v-model="ruleForm.accidentAmountThreeYears" disabled
                        v-thousand
                        v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ format_thousand(ruleForm.accidentAmountThreeYears) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="指导价(元):" prop="guidancePrice">
              <el-input v-model="ruleForm.guidancePrice" disabled
                        v-thousand
                        v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ format_thousand(ruleForm.guidancePrice) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购价(元):" prop="purchasePrice">
              <el-input v-model="ruleForm.purchasePrice" disabled
                        v-thousand
                        v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ format_thousand(ruleForm.purchasePrice) }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="初期残值(元):" prop="initialResidualValue">
              <el-input v-model="ruleForm.initialResidualValue" disabled
                        v-thousand
                        v-if="isEdit" placeholder="请选择车辆后自动带出" />
              <span v-else>{{ format_thousand(ruleForm.initialResidualValue) }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="公里数:" prop="mileage">
              <el-input v-model="ruleForm.mileage" v-if="isEdit" maxlength="8"
                        @change="handleChange('mileage')" placeholder="请输入公里数" />
              <span v-else>{{ ruleForm.mileage }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="过户次数:" prop="transferCount">
              <el-input v-model="ruleForm.transferCount" v-if="isEdit" maxlength="8"
                        @change="handleChange('transferCount')" placeholder="请输入过户次数" />
              <span v-else>{{ ruleForm.transferCount }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="车况等级:" prop="vehicleConditionLevel">
              <el-select v-model="ruleForm.vehicleConditionLevel" style="width: 100%" v-if="isEdit"
                placeholder="请选择车况等级" clearable>
                <el-option v-for="dict in dict.type.vehicle_condition_level_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
              <span v-else>{{ ruleForm.vehicleConditionLevel }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆停放城市:" prop="cityName">
              <el-input v-model="ruleForm.cityName" v-if="isEdit" placeholder="请输入车辆停放城市" />
              <span v-else>{{ ruleForm.cityName }}</span>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="处置原因:" prop="disposalReason">
              <el-input v-if="isEdit" type="textarea" v-model="ruleForm.disposalReason" placeholder="请输入处置原因" :rows="4"
                maxlength="500" show-word-limit />
              <p v-else>{{ ruleForm.disposalReason }}</p>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="车辆异常:" prop="vehicleStatus">
              <el-input v-if="isEdit" type="textarea" v-model="ruleForm.vehicleStatus" placeholder="请输入车辆异常" :rows="4"
                maxlength="500" show-word-limit />
              <p v-else>{{ ruleForm.vehicleStatus }}</p>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="附件:" prop="attachment">
              <image-common v-model="ruleForm.attachment" :limit="20" :disabled="!isEdit" :upType="2" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <car-table ref="refCarTable" @handleSelect="handleSelect" />
  </div>

</template>

<script>
export default {
  name: "NewApplication", //申请信息
  dicts: ["vehicle_condition_level_type", "fuel_type","car_nature"],
  props: {
    value: {
      type: Object,
      default: () => {},
    },
    state: {
      type: String,
      default: "",
    },
  },
  components: {
    carTable: () => import("./carTable.vue"),
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            if (value) {
              that.handleClearValidate(key);
            }
            that.$emit("input", { ...target, [key]: value });
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === "write") {
        return "top";
      } else {
        return "right";
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool;
    },
  },
  watch: {
    'value.carNo':{
      handler(val){
        if(val){
          this.handleClearValidate('carNo')
        }
      },
      immediate:true,
      deep:true
    }
  },
  data() {
    return {
      rules: {
        carNo: [
          {
            required: this.state === "write",
            message: "车牌号不能为空",
            trigger: "blur",
          },
        ],
        mileage: [
          {
            required: this.state === "write",
            message: "公里数不能为空",
            trigger: "blur",
          },
        ],
        transferCount: [
          {
            required: this.state === "write",
            message: "过户次数不能为空",
            trigger: "blur",
          },
        ],
        vehicleConditionLevel: [
          {
            required: this.state === "write",
            message: "车况等级不能为空",
            trigger: "blur",
          },
        ],
        cityName: [
          {
            required: this.state === "write",
            message: "车辆停放城市不能为空",
            trigger: "blur",
          },
        ],
        disposalReason:[
          {
            required: this.state === "write",
            message: "处置原因不能为空",
            trigger: "blur",
          },
        ],
        vehicleStatus:[
          {
            required: this.state === "write",
            message: "车辆异常不能为空",
            trigger: "blur",
          },
        ]
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    handleFocus() {
      this.$refs.refCarTable.handleOpen();
    },

    handleSelect(val) {
      console.log(val);
      if (!val) return;
      for (let key in this.ruleForm) {
        this.$set(this.value, key, val[key]);
      }
    },

    handleChange(str){
      let regex = /^-?[1-9]\d*$/
      if(!regex.test(this.ruleForm[str])){
        this.ruleForm[str] = ''
      }else{
        this.ruleForm[str] =  this.ruleForm[str]
      }
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            this.locateToErr();
            reject();
          }
        });
      });
    },

    locateToErr() {
      this.$nextTick(() => {
        let isError = document.getElementsByClassName("is-error");
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: "center",
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: "smooth",
        });
      });
    },

    //取消表单效验
    handleClearValidate(str) {
      this.$refs.ruleForm.clearValidate(str);
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";
</style>


